class_name PlayerInputHandler
extends Node

var movement_input: float = 0.0

var idle_input: bool = false

var crouch_input: bool = false

var walk_input: bool = false

var run_input: bool = false

var jump_input: bool = false

var jump_keep_input: bool = false

var climb_input: bool = false

var cancel_climb_input: bool = false

var normal_attack_input: bool = false

var normal_attack_keep_input: bool = false

var _idle_threshold_speed: float = 0.1

var _run_keep_threshold_time: float = 0.2

var _last_run_end_time: float = 0.0

@onready var move_left_checker: InputActionChecker = %MoveLeftChecker

@onready var move_right_checker: InputActionChecker = %MoveRightChecker

@onready var jump_checker: InputActionChecker = %JumpChecker

@onready var normal_attack_checker: InputActionChecker = %NormalAttackChecker


func _ready() -> void:
	move_left_checker.single_click_started.connect(_on_move_action_single_click_started)
	move_left_checker.double_click_started.connect(_on_move_action_double_click_started)
	move_left_checker.released.connect(_on_move_action_released)

	move_right_checker.single_click_started.connect(_on_move_action_single_click_started)
	move_right_checker.double_click_started.connect(_on_move_action_double_click_started)
	move_right_checker.released.connect(_on_move_action_released)

	jump_checker.single_click_started.connect(_on_jump_action_single_click_started)
	jump_checker.long_pressed.connect(_on_jump_action_long_pressed)
	jump_checker.released.connect(_on_jump_action_released)

	normal_attack_checker.single_click_started.connect(_on_normal_attack_action_single_click_started)
	normal_attack_checker.long_pressed.connect(_on_normal_attack_action_long_pressed)
	normal_attack_checker.released.connect(_on_normal_attack_action_released)


func on_update(delta: float) -> void:
	move_left_checker.on_update(delta)
	move_right_checker.on_update(delta)
	jump_checker.on_update(delta)
	normal_attack_checker.on_update(delta)
	
	movement_input = Input.get_axis("move_left", "move_right")
	crouch_input = Input.is_action_pressed("crouch")
	idle_input = movement_input < _idle_threshold_speed and movement_input > -_idle_threshold_speed
	walk_input = !idle_input and !run_input
	climb_input = Input.is_action_pressed("climb")
	cancel_climb_input = Input.is_action_just_pressed("cancel_climb")


func _on_move_action_single_click_started(current_time: float) -> void:
	if current_time - _last_run_end_time < _run_keep_threshold_time:
		run_input = true


func _on_move_action_double_click_started(_current_time: float) -> void:
	run_input = true


func _on_move_action_released(current_time: float) -> void:
	if run_input:
		_last_run_end_time = current_time
	run_input = false


func _on_jump_action_single_click_started(_current_time: float) -> void:
	jump_input = true


func _on_jump_action_long_pressed(_current_time: float) -> void:
	jump_keep_input = true


func _on_jump_action_released(_current_time: float) -> void:
	jump_input = false
	jump_keep_input = false


func _on_normal_attack_action_single_click_started(_current_time: float) -> void:
	normal_attack_input = true


func _on_normal_attack_action_long_pressed(_current_time: float) -> void:
	normal_attack_keep_input = true


func _on_normal_attack_action_released(_current_time: float) -> void:
	normal_attack_input = false
	normal_attack_keep_input = false
